<?php
/**
 * Created by JetBrains PhpStorm.
 * User: 蒋文书
 * Date: 14-12-27
 * Time: 下午11:27
 * To change this template use File | Settings | File Templates.
 */

namespace Admin\Controller;
use Think\Controller;


class PublicController extends Controller{

    public function index(){
        $this->meta_title = '后台登陆';
        $this->display();
    }

    /**
     * 登陆
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function login(){
        if(!IS_POST) $this->error('页面不存在');

        $username=I('username');
        $password=I('password','','md5');
        $user=M('user')->where(array('username'=>$username))->find();
        if(!$user || $user['password'] != $password){
            $this->error('账号或密码错误');
        }

        if($user['status'] == 0) $this->error('用户被锁定');

        $data =array(
            'id'=>$user['id'],
            'last_login_time'=>time(),
            'last_login_ip'=>get_client_ip()
        );
        M('user')->save($data);

        $_SESSION['user_info'] = $user;
        $_SESSION[C('USER_AUTH_KEY')] = $user['id'];
        $_SESSION['last_login_time'] = $user['last_login_time'];
        $_SESSION['last_login_ip'] = $user['last_login_ip'];
        if($user['username'] == C('RBAC_SUPERADMIN')){
             $_SESSION[C('ADMIN_AUTH_KEY')]=true;
        }
       
        $RBAC = new \Org\Util\Rbac();
        $RBAC::saveAccessList();
        $this->meta_title = '后台登陆';
        $this->redirect('Admin/Along/Index');
    }

    /**
     * 退出登录
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function logout(){
        session_unset();
        session_destroy();
        $this->redirect('Admin/Public/Index');
    }

}